package ylg.cn.com.percentlayoutdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

/**
 * Created by Administrator on 2017/10/25.
 */

public class PercentRelayout extends RelativeLayout {
    public PercentRelayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public PercentRelayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public PercentRelayout(Context context) {
        super(context);
    }

    //测量容器的宽和高;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        //父容器的
        int width = View.MeasureSpec.getSize(widthMeasureSpec);
        int heigh = View.MeasureSpec.getSize(heightMeasureSpec);
        //测量子空间的宽高
        for (int i = 0;i<getChildCount();i++){
            float percentwidth = 0 ;
            float percenthight = 0;
            View child = getChildAt(i);
            ViewGroup.LayoutParams params = child.getLayoutParams();
            if (params instanceof PercentRelayout.LayoutParams){
                percentwidth = ((PercentRelayout.LayoutParams) params).getWithpercent();
                percenthight = ((PercentRelayout.LayoutParams) params).getHeightpercent();
            }
            if (percentwidth!=0) {
                params.width = (int) (width*percentwidth);
                params.height = (int) (heigh*percenthight);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new LayoutParams(getContext(),attrs);
    }


    public class LayoutParams extends RelativeLayout.LayoutParams{

        private float withpercent;

        private float heightpercent;

        public float getHeightpercent() {
            return heightpercent;
        }

        public void setHeightpercent(float heightpercent) {
            this.heightpercent = heightpercent;
        }

        public float getWithpercent() {
            return withpercent;
        }

        public void setWithpercent(float withpercent) {
            this.withpercent = withpercent;
        }

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            TypedArray array = c.obtainStyledAttributes(attrs,R.styleable.PercentLayout);
            withpercent = array.getFloat(R.styleable.PercentLayout_percent_widthlayout,withpercent);
            heightpercent = array.getFloat(R.styleable.PercentLayout_percent_heightlayout,heightpercent);
            array.recycle();//释放资源;
        }

        public LayoutParams(int w, int h) {
            super(w, h);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }

        public LayoutParams(MarginLayoutParams source) {
            super(source);
        }
    }
}
